/**
 * Copyright &copy; 2016-2017 <a href="http://git.oschina.net/whoamien/backend_management">Backend Management</a> All rights reserved.
 */
package com.xw.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.lang3.StringUtils;

/**
 * The Class ConvertUtils.
 *
 * @author Xi Wei
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ConvertUtils {

	private static final int KB = 1024;
	static {
		registerDateConverter();
	}

	/**
	 * Convert element property to list.
	 *
	 * @param collection the collection
	 * @param propertyName the property name
	 * @return the list
	 */
	public static List convertElementPropertyToList(Collection collection, String propertyName) {
		List list = new ArrayList();
		try {
			for (Iterator<?> localIterator = collection.iterator(); localIterator.hasNext();) {
				Object obj = localIterator.next();
				list.add(PropertyUtils.getProperty(obj, propertyName));
			}
		} catch (Exception e) {
			throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
		}
		return list;
	}

	/**
	 * Convert element property to string.
	 *
	 * @param collection the collection
	 * @param propertyName the property name
	 * @param separator the separator
	 * @return the string
	 */
	public static String convertElementPropertyToString(Collection collection, String propertyName, String separator) {
		List list = convertElementPropertyToList(collection, propertyName);
		return StringUtils.join(list, separator);
	}

	/**
	 * Convert string to object.
	 *
	 * @param value the value
	 * @param toType the to type
	 * @return the object
	 */
	public static Object convertStringToObject(String value, Class<?> toType) {
		try {
			return org.apache.commons.beanutils.ConvertUtils.convert(value, toType);
		} catch (Exception e) {
			throw ReflectionUtils.convertReflectionExceptionToUnchecked(e);
		}

	}

	/**
	 * 读取txt文件的内容.
	 *
	 * @param file            想要读取的文件对象
	 * @return 返回文件内容
	 */
	public static String txt2String(File file) {
		String result = "";
		try {
			BufferedReader br = new BufferedReader(new FileReader(file));// 构造一个BufferedReader类来读取文件
			String s = null;
			while ((s = br.readLine()) != null) {// 使用readLine方法，一次读一行
				System.out.println(System.lineSeparator());
				result += s;
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.toString();
	}

	/**
	 * 读取txt文件的内容.
	 *
	 * @param is the is
	 * @return 返回文件内容
	 */
	public static String txt2String(InputStream is) {
		String result = "";
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(is));// 构造一个BufferedReader类来读取文件
			String s = null;
			while ((s = br.readLine()) != null) {// 使用readLine方法，一次读一行
				System.out.println(System.lineSeparator());
				result += s + System.lineSeparator();
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result.toString();
	}

	/**
	 * inputStream转outputStream.
	 *
	 * @param is            输入流
	 * @return outputStream子类
	 */
	public static ByteArrayOutputStream input2OutputStream(InputStream is) {
		if (is == null)
			return null;
		try {
			ByteArrayOutputStream os = new ByteArrayOutputStream();
			byte[] b = new byte[KB];
			int len;
			while ((len = is.read(b, 0, KB)) != -1) {
				os.write(b, 0, len);
			}
			return os;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			FileUtil.closeIO(is);
		}
	}

	/**
	 * inputStream转byteArr.
	 *
	 * @param is            输入流
	 * @return 字节数组
	 */
	public static byte[] inputStream2Bytes(InputStream is) {
		if (is == null)
			return null;
		return input2OutputStream(is).toByteArray();
	}

	private static void registerDateConverter() {
		DateConverter dc = new DateConverter();
		dc.setUseLocaleFormat(true);
		dc.setPatterns(new String[] { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss" });
		org.apache.commons.beanutils.ConvertUtils.register(dc, Date.class);
	}
}
